﻿<%@ WebHandler Language="C#" Class="VerifyImage" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Web.SessionState;

public class VerifyImage : IHttpHandler,IRequiresSessionState
{

    private string GetRandomNumberString(int codeCount)
    {
        int j1;
        string strChoice = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
        string[] strResult = strChoice.Split(new Char[] { ',' });
        string strReturn = "";
        Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
        for (int i = 0; i < codeCount; i++)
        {
            Random rnd1 = new Random(rnd.Next() * unchecked((int)DateTime.Now.Ticks));
            j1 = rnd1.Next(strResult.Length);
            rnd = new Random(rnd.Next() * unchecked((int)DateTime.Now.Ticks));
            strReturn = strReturn + strResult[j1].ToString();
        }
        return strReturn;
    }

    Color[] color = new Color[] { Color.Blue, Color.Red,Color.Brown, Color.Green ,Color.Black};
    private Color GetColor(int index)
    {
        return color[index];
    }

    private Bitmap CreateImage(string str_ValidateCode)
    {
        Random newRandom = new Random((int)DateTime.Now.Ticks);
        int int_ImageWidth = str_ValidateCode.Length * 14;
        int int_ImageHeight = 20;
        //  图高20px
        Bitmap theBitmap = new Bitmap(int_ImageWidth, int_ImageHeight);
        Graphics theGraphics = Graphics.FromImage(theBitmap);
        //  白色背景
        theGraphics.Clear(Color.White);
        //  灰色边框
        theGraphics.DrawRectangle(new Pen(Color.White, 1), 0, 0, int_ImageWidth - 1+5, int_ImageHeight);

        int num = theBitmap.Width * theBitmap.Height * 30 / 100;
        for (int iCount = 0; iCount < num; iCount++)
        {
            // 在随机的位置使用随机的颜色设置图片的像素
            int x = newRandom.Next(theBitmap.Width);
            int y = newRandom.Next(theBitmap.Height);
            int r = newRandom.Next(255);
            int g = newRandom.Next(255);
            int b = newRandom.Next(255);
            Color c = Color.FromArgb(r, g, b);
            //Color c = Color.White;
            theBitmap.SetPixel(x, y, c);
        }//for


        //  10pt的字体
        Font theFont = new Font("Arial", 12, FontStyle.Bold);

        Random ran = new Random();
        for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
        {
            string str_char = str_ValidateCode.Substring(int_index, 1);
            Brush newBrush = new SolidBrush(GetColor(ran.Next(color.Length)));
            Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
            theGraphics.DrawString(str_char, theFont, newBrush, thePos);
        }

        theGraphics.Dispose();
        return theBitmap;
    }

    
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        HttpResponse Response = context.Response;
        //  将生成的图片发回客户端
        MemoryStream ms = new MemoryStream();
        string NumStr = GetRandomNumberString(4);
        context.Session.Add("Verify", NumStr);

        Bitmap theBitmap = CreateImage(NumStr);
        theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent(); //需要输出图象信息 要修改HTTP头 
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(ms.ToArray());
        theBitmap.Dispose();
        ms.Close();
        ms.Dispose();
        Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}